const {
    getRoleById,    // 根据id查询角色    
    getRoleList,    // 查询角色列表    
    createRole,     // 添加角色
    editRole,       // 编辑角色
    removeRole,     // 删除角色
} = require("../../service/user/role")


exports.roleApi = async ctx => {
    // 获取url 字段 继而判断接下来操作
    let url = ctx.request.url.split("/")[1];
    const { id } = ctx.params || "";
    const { rolename, rolekey, creator, pageNum, pageSize, rolestatus, isdelete, remark, updator } = ctx.request.body || "";
    let res;
    switch (ctx.request.method) {
        case "GET": if (ctx.request.url.length > url.length + 1) {
            res = await getRoleById({id})
        } else {
            res = await getRoleList({ pageNum, pageSize })
        }; break;
        case "POST": res = await createRole({ rolename, rolekey, creator }); break;
        case "DELETE": res = await removeRole({ id }); break;
        case "PUT": res = await editRole({ id, rolename, rolekey, rolestatus, isdelete, remark, updator }); break;
        default: res = {msg:"操作失败", code:"400"}
    }
    ctx.body = res
}